package com.model.base;

import com.common.ErrorCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author: zql
 * @Description:
 * @Data: 14:16 2021-06-21
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@ApiModel("Result")
public class Result<T> {
    /**
     * 1.status状态值：代表本次请求response的状态结果。
     */
    @ApiModelProperty(value = "状态码",required = true)
    private String code;
    /**
     * 2.response描述：对本次状态码的描述。
     */
    @ApiModelProperty(value = "描述",required = true)
    private String message;
    /**
     * 3.data数据：本次返回的数据。
     */
    @ApiModelProperty(value = "数据",required = true)
    private T data;

    /**
     * 成功，创建ResResult：没data数据
     */
    public static <T> Result<T> suc() {
        Result<T> result = new Result<>();
        result.setResultCode(ErrorCodeEnum.SUCCESS);
        return result;
    }

    /**
     * 成功，创建ResResult：有data数据
     */
    public static <T> Result<T> suc(T data) {
        Result<T> result = new Result<>();
        result.setResultCode(ErrorCodeEnum.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 失败，指定status、desc
     */
    public static <T> Result<T> fail(String status, String desc) {
        Result<T> result = new Result<>();
        result.setCode(status);
        result.setMessage(desc);
        return result;
    }

    /**
     * 失败，指定ResultCode枚举
     */
    public static <T> Result<T> fail(ErrorCodeEnum resultCode) {
        Result<T> result = new Result<>();
        result.setResultCode(resultCode);
        return result;
    }

    /**
     * 失败，指定ResultCode枚举，返回错误参数
     */
    public static <T> Result<T> fail(ErrorCodeEnum resultCode,T data) {
        Result<T> result = new Result<>();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    /**
     * 把ResultCode枚举转换为ResResult
     */
    private void setResultCode(ErrorCodeEnum code) {
        this.code = code.getErrorCode();
        this.message = code.getErrorMsg();
    }
}

